package com.ruoyi.commissionrule.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.binding.domain.AmRuleBinding;
import com.ruoyi.binding.mapper.AmRuleBindingMapper;
import com.ruoyi.building.domain.AmBuilding;
import com.ruoyi.commission.domain.AmCommission;
import com.ruoyi.commission.mapper.AmCommissionMapper;
import com.ruoyi.commissionrule.domain.vo.AmCommissionRuleVo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.housingresources.domain.AmHousingResources;
import com.ruoyi.housingresources.mapper.AmHousingResourcesMapper;
import com.ruoyi.project.domain.AmProject;
import com.ruoyi.project.mapper.AmProjectMapper;
import com.ruoyi.project.service.IAmProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.commissionrule.mapper.AmCommissionRuleMapper;
import com.ruoyi.commissionrule.domain.AmCommissionRule;
import com.ruoyi.commissionrule.service.IAmCommissionRuleService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 分佣机制管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-07-28
 */
@Service
public class AmCommissionRuleServiceImpl implements IAmCommissionRuleService{
    @Resource
    private AmCommissionRuleMapper amCommissionRuleMapper;

    @Resource
    private IAmProjectService projectService;

    @Resource
    private AmCommissionMapper amCommissionMapper;

    @Resource
    private AmRuleBindingMapper amRuleBindingMapper;


    /**
     * 查询分佣机制管理
     * 
     * @param id 分佣机制管理ID
     * @return 分佣机制管理
     */
    @Override
    public HashMap<String, Object> selectAmCommissionRuleById(Long id)
    {
        HashMap<String,Object> results = new HashMap<String, Object>(16);
            results.put("id",id);

            // 查询返佣规则基础信息
            results.put("amCommissionRule",amCommissionRuleMapper.selectAmCommissionRuleById(id));
            // 查询返佣占比信息
            AmCommission amCommission = new AmCommission();
            amCommission.setRuleId(id);
            amCommissionMapper.list(amCommission).forEach(commission ->{
                switch (commission.getGrade()){
                    case "A" :
                        results.put("commissionRateA",commission);
                        break;
                    case "B" :
                        results.put("commissionRateB",commission);
                        break;
                    case "C" :
                        results.put("commissionRateC",commission);
                        break;
                    default:
                        break;
                }
            });
            // 查询该佣金规则已绑定房源

            // 查询房源绑定规则
            AmRuleBinding amRuleBinding = new AmRuleBinding();
            amRuleBinding.setCommissionRuleId(id);
            // 项目树
		List<HashMap<String, Object>> projectTree = projectService.getProjectTree(id);
		results.put("amRuleBindings", amRuleBindingMapper.list(amRuleBinding));
        results.put("projectTree",projectTree.get(0).get("projectTree"));
        results.put("checked",projectTree.get(0).get("checked"));
        return results;
    }

    /**
     * 查询分佣机制管理列表
     * 
     * @param amCommissionRule 分佣机制管理
     * @return 分佣机制管理
     */
    @Override
    public List<AmCommissionRule> selectAmCommissionRuleList(AmCommissionRule amCommissionRule)
    {
        return amCommissionRuleMapper.selectAmCommissionRuleList(amCommissionRule);
    }

    /**
     * 新增分佣机制管理
     * 
     * @param amCommissionRule 分佣机制管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertAmCommissionRule(AmCommissionRuleVo amCommissionRuleVo)
    {
        AmCommission commissionRateA = amCommissionRuleVo.getCommissionRateA();
        AmCommission commissionRateB = amCommissionRuleVo.getCommissionRateB();
        AmCommission commissionRateC = amCommissionRuleVo.getCommissionRateC();

        
        // 插入返佣规则基础信息
        amCommissionRuleMapper.insertAmCommissionRule(amCommissionRuleVo.getAmCommissionRule());
        Long ruleId = amCommissionRuleVo.getAmCommissionRule().getId();

        //佣金规则绑定
        commissionRateA.setRuleId(ruleId);
        commissionRateB.setRuleId(ruleId);
        commissionRateC.setRuleId(ruleId);

        //插入返佣规则佣金占比
        amCommissionMapper.insertAmCommission(commissionRateA);
        amCommissionMapper.insertAmCommission(commissionRateB);
        amCommissionMapper.insertAmCommission(commissionRateC);


        // 佣金规则绑定
        List<AmRuleBinding> list = new ArrayList<AmRuleBinding>();
        if (null != amCommissionRuleVo.getAmRuleBindings() && amCommissionRuleVo.getAmRuleBindings().size() > 0 ) {
            amCommissionRuleVo.getAmRuleBindings().forEach( amRuleBinding -> {
               list.add(new AmRuleBinding(amRuleBinding , ruleId));
            });
            amRuleBindingMapper.batchInsert(list);
        }

        return 1;
    }

    /**
     * 修改分佣机制管理
     * 
     * @param amCommissionRule 分佣机制管理
     * @return 结果
     */
    @Override
    public int updateAmCommissionRule(AmCommissionRuleVo amCommissionRuleVo)
    {

        // 返佣规则信息表id
        Long amCommissionRuleId = amCommissionRuleVo.getAmCommissionRule().getId();
        // 更新返佣规则基础信息
        amCommissionRuleMapper.updateAmCommissionRule(amCommissionRuleVo.getAmCommissionRule());
        // 删除 房源返佣规则关系表
        amRuleBindingMapper.deleteByRuleId(amCommissionRuleId);
        // 删除 返佣规则占比表
        amCommissionMapper.deleteByRuleId(amCommissionRuleId);
        //佣金规则绑定
        amCommissionRuleVo.getCommissionRateA().setRuleId(amCommissionRuleId);
        amCommissionRuleVo.getCommissionRateB().setRuleId(amCommissionRuleId);
        amCommissionRuleVo.getCommissionRateC().setRuleId(amCommissionRuleId);
        //插入返佣规则佣金占比
        amCommissionMapper.insertAmCommission(amCommissionRuleVo.getCommissionRateA());
        amCommissionMapper.insertAmCommission(amCommissionRuleVo.getCommissionRateB());
        amCommissionMapper.insertAmCommission(amCommissionRuleVo.getCommissionRateC());
        // 佣金规则绑定
        if(null != amCommissionRuleVo.getAmRuleBindings() && amCommissionRuleVo.getAmRuleBindings().size() > 0 ) {
            List<AmRuleBinding> list = new ArrayList<AmRuleBinding>();
            amCommissionRuleVo.getAmRuleBindings().forEach(housingId ->{
                list.add(new AmRuleBinding(housingId,amCommissionRuleId));
            });
            amRuleBindingMapper.batchInsert(list);
        }

        return 1;
    }

    /**
     * 批量删除分佣机制管理
     * 
     * @param ids 需要删除的分佣机制管理ID
     * @return 结果
     */
    @Override
    public int deleteAmCommissionRuleByIds(Long[] ids)
    {
        // 先删除 房源返佣规则关系表
        amRuleBindingMapper.deleteByRuleIds(ids);

        //再删除 返佣规则占比表
        amCommissionMapper.deleteByRuleIds(ids);

        //最后删除返佣规则表
        amCommissionRuleMapper.deleteAmCommissionRuleByIds(ids);

        return 1;
    }

    /**
     * 删除分佣机制管理信息
     * 
     * @param id 分佣机制管理ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteAmCommissionRuleById(Long id)
    {
        // 先删除 房源返佣规则关系表
        amRuleBindingMapper.deleteByRuleId(id);

        //再删除 返佣规则占比表
        amCommissionMapper.deleteByRuleId(id);

        //最后删除返佣规则表
        amCommissionRuleMapper.deleteAmCommissionRuleById(id);
        return 1;
    }
}
